﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FW.Core.Model;
using FW.Core.Repository;

namespace FW.Data
{
    public class Repository<T> : IRepository<T> where T : Entity, new()
    {
        private readonly DbContext _dbContext;
        protected DbSet<T> DbSet;

        public Repository(IDbContextFactory contextFactory)
        {
            _dbContext = contextFactory.GetContext();
            DbSet = _dbContext.Set<T>();
        }

        public IQueryable<T> GetAll()
        {
            return DbSet;
        }

        public void Delete(T t)
        {
            DbSet.Remove(t);
        }

        public void Insert(T t)
        {
            DbSet.Add(t);
        }

        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }
    }
}
